package cn.laixueit.service;

import cn.laixueit.mapper.StudentMapper;
import cn.laixueit.pojo.Student;
import cn.laixueit.pojo.StudentExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public PageInfo<Student> findPage(Integer start,Integer size,Student student){
        PageHelper.startPage(start,size);
        List<Student> studentList = studentMapper.selectByExample(getExample(student));
        PageInfo<Student> pageInfo = new PageInfo<>(studentList);
        return pageInfo;
    }

    private StudentExample getExample(Student student) {
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        if(student.getName()!=null){
            criteria.andNameLike("%"+student.getName()+"%");
        }
        return example;
    }

    public void save(Student student) {
        studentMapper.insertSelective(student);
    }

    public Student findById(Integer id) {
       return  studentMapper.selectByPrimaryKey(id);
    }

    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }
}
